/*
 * @Author: your name
 * @Date: 2021-06-04 15:21:41
 * @LastEditTime: 2021-06-04 15:55:12
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bazai_island_api\app\models\book-comment.js
 */
const { Sequelize, Model } = require('sequelize')
const { sequelize } = require('@core/mysqlDB')


class Comment extends Model {
    static async addComment(book_id, content) {
        // 点赞 + 1, 你好酷 + 1
        // 判断是否有近似
        const comment = await Comment.findOne({
            where: {
                book_id,
                content
            }
        })
        if (!comment) {
            return await Comment.create({
                book_id,
                content,
                nums: 1
            })
        } else {
            // 存在评论数据，进行递增
            return await comment.increment('nums', {
                by: 1
            })
        }
    }

    static async getComment(book_id) {
        // 点赞 + 1, 你好酷 + 1
        // 判断是否有近似
        const comment = await Comment.findAll({
            where: {
                book_id
            }
        })
        return comment
    }
}

Comment.init({
    content: Sequelize.STRING(48),
    nums: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    },
    book_id: Sequelize.INTEGER
}, {
    sequelize,
    tableName: 'comment'
})

module.exports = {
    Comment
}